package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook 地址簿
     * @return R
     */
    @PostMapping
    public Result<String> add(@RequestBody AddressBook addressBook) {
        addressBookService.add(addressBook);
        return Result.success();
    }

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return R
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> selectByUserId() {
        List<AddressBook> addressBook = addressBookService.selectByUserId();
        return Result.success(addressBook);
    }

    /**
     * 根据id查询地址
     *
     * @param id 地址id
     * @return R
     */
    @GetMapping("/{id}")
    public Result<AddressBook> selectById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.selectById(id);
        return Result.success(addressBook);
    }


    /**
     * 根据id修改地址
     *
     * @param addressBook 地址信息
     * @return R
     */
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param id 地址id
     * @return R
     */
    @DeleteMapping
    public Result<String> deleteById(Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询默认地址
     *
     * @return R
     */
    @GetMapping("/default")
    public Result<AddressBook> selectDefault() {
        AddressBook addressBook = addressBookService.selectDefault();
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook 地址信息
     * @return R
     */
    @PutMapping("/default")
    public Result<String> settingDefaultDddress(@RequestBody AddressBook addressBook ) {
        addressBookService.settingDefaultDddress(addressBook);
        return Result.success();
    }
}
